
import { Post, Prefix } from 'egg-shell-decorators';
import BaseController from '../../base';


@Prefix('api/v1/user')
export default class UserController extends BaseController {

  @Post('/login')
  public async login() {
    const { ctx, app } = this;
    const user = ctx.request.body;
    if (user && user.username) {
      const userToken = {
        username: user.username,
      };
      const token = app.jwt.sign(userToken, app.config.jwt.secret, { expiresIn: '1h' }); // token签名 有效期为1小时
      ctx.set({ authorization: token }); // 设置 headers
      this.success(user);

    } else {
      this.error({
        msg: '登录失败',
      });
    }
  }


}
